package com.isoft.airportsystem.controller;

import com.isoft.airportsystem.common.Result;
import com.isoft.airportsystem.entity.flight.BoardingGateInfo;
import com.isoft.airportsystem.service.flight.BoardingGateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/boarding-gate")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true")
public class BoardingGateController {
    @Autowired
    private BoardingGateService boardingGateService;

    // 按航班号+日期查询
    @GetMapping("/search")
    public Result<BoardingGateInfo> getByFlightNoAndDate(
            @RequestParam String flightNumber,
            @RequestParam String flightDate
    ) {
        BoardingGateInfo info = boardingGateService.getByFlightNoAndDate(flightNumber, flightDate);
        return info != null ? Result.success(info) : Result.error("未找到登机口信息");
    }

    // 按航班ID查询
    @GetMapping("/{flightId}")
    public Result<BoardingGateInfo> getByFlightId(@PathVariable String flightId) {
        BoardingGateInfo info = boardingGateService.getByFlightId(flightId);
        return info != null ? Result.success(info) : Result.error("未找到登机口信息");
    }
    @GetMapping
    public Result<List<BoardingGateInfo>> getAllBoardingGates() {
        List<BoardingGateInfo> list = boardingGateService.findAllBoardingGates();
        return Result.success(list); // 确保返回的是List
    }
    // 新增登机口
    @PostMapping
    public Result<Void> addBoardingGate(@RequestBody BoardingGateInfo info) {
        boolean success = boardingGateService.addBoardingGate(info);
        return success ? Result.success() : Result.error("新增失败");
    }

    // 修改登机口
    @PutMapping
    public Result<Void> updateBoardingGate(@RequestBody BoardingGateInfo info) {
        boolean success = boardingGateService.updateBoardingGate(info);
        return success ? Result.success() : Result.error("修改失败");
    }

    // 删除登机口
    @DeleteMapping("/{flightId}")
    public Result<Void> deleteBoardingGate(@PathVariable String flightId) {
        boolean success = boardingGateService.deleteBoardingGate(flightId);
        return success ? Result.success() : Result.error("删除失败");
    }

    // 按登机口编码查询
    @GetMapping("/by-gate")
    public Result<List<BoardingGateInfo>> getByBoardingGate(@RequestParam String boardingGate) {
        List<BoardingGateInfo> list = boardingGateService.getByBoardingGate(boardingGate);
        return Result.success(list);
    }

    // 按日期查询
    @GetMapping("/by-date")
    public Result<List<BoardingGateInfo>> getByDate(@RequestParam String flightDate) {
        List<BoardingGateInfo> list = boardingGateService.getByDate(flightDate);
        return Result.success(list);
    }
}